package jtmcraft.game.units.states;

import jtmcraft.game.units.CollisionManager;
import jtmcraft.game.units.GameUnit;
import jtmcraft.game.units.WorkerUnit;
import jtmcraft.graphics.Sprite;

/**
 *
 * @author milosz
 */
public class GatherState extends MovableState {

    private GameUnit mine;
    private GameUnit home;
    private GameUnit destination;

    private int sleepCount = 0;
    private int sleepDelay = 10;

    public GatherState(GameUnit mine, GameUnit home) {
        this.mine = mine;
        this.home = home;
        destination = mine;
    }

    public void update(GameUnit gameUnit, long timestamp) {

        if( sleepCount > 0 )
        {
            //gameUnit -> setinvisible
            sleepCount--;
            return;
        }

        gameUnit.setInvisible(false);

        int dx = destination.getX() + destination.getWidth() / 2 - (gameUnit.getWidth() / 2) - gameUnit.getX();
        int dy = destination.getY() + destination.getHeight() / 2 - (gameUnit.getHeight() / 2) - gameUnit.getY();


        if (dx == 0 && dy == 0) {
            return;
        }

        double dr = Math.sqrt(dx * dx + dy * dy);

        double i = dx / dr;
        double j = dy / dr;

        double sx = (i * .05 * timestamp);
        double sy = (j * .05 * timestamp);

        //to jes s
        if (Math.abs(sx) > Math.abs(dx)) {
            gameUnit.setX(gameUnit.getX() + (int) dx);
            gameUnit.setY(gameUnit.getY() + (int) dy);

            return;
        }

        int newX = gameUnit.getX() + (int) sx;
        int newY = gameUnit.getY() + (int) sy;

        // gameUnit.setX(newX);
        // gameUnit.setY(newY);

        double katRadian = Math.asin(j);

        Sprite sprite = gameUnit.getSprite();

        setMoveAction(sprite, katRadian, i);


        /// wczesniejsze mozna wydzielic

        GameUnit collisionUnit = CollisionManager.getInstance().checkCollision(newX, newY, gameUnit);

        if (null == collisionUnit) {
            gameUnit.setX(newX);
            gameUnit.setY(newY);
            return;
        }

        if (collisionUnit == destination) {
            if( home == destination ) {
                gameUnit.getActionHandler().addResource( 5 );
            }
            gameUnit.setInvisible(true);
            switchDestination( gameUnit );
            return;
        }

        //jezeli nie to omijamy

            for (int n = 45; n < 360; n += 45) {
                int prX = (int) (sx * Math.cos(Math.toRadians(n)) - sy * Math.sin(Math.toRadians(n)));
                int prY = (int) (sx * Math.sin(Math.toRadians(n)) + sy * Math.cos(Math.toRadians(n)));
                // remap to new position
                newX = prX + gameUnit.getX(); // slight speed up to avoid deadlock
                newY = prY + gameUnit.getY(); // slight speed up to avoid deadlock
                if (null == CollisionManager.getInstance().checkCollision(newX, newY, gameUnit)) {
                    gameUnit.setX(newX);
                    gameUnit.setY(newY);

                    dr = Math.sqrt(prX * prX + prY * prY);
                    i = prX / dr;
                    j = prY / dr;
                    katRadian = Math.asin(j);
                    setMoveAction(sprite, katRadian, i);

                    break;
                }
            }
    }

    private void switchDestination(GameUnit gameUnit) {

        if( gameUnit.getClass().equals( WorkerUnit.class ) )
        {
            WorkerUnit workerUnit = (WorkerUnit) gameUnit;
            workerUnit.setHaveGold( destination == mine );
            workerUnit.getSprite().setMove(true);
        }

        destination = destination == mine ? home : mine;
        sleepCount = sleepDelay;
       
    }
}
